#include "MapperGxROM.h"
#include "Log.h"
PNE_NAMESPACE_BEGIN

MapperGxROM::MapperGxROM(Cartridge &cart,
                         std::function<void(void)> mirroring_callback)
    : Mapper(cart, Mapper::Type::GxROM),
      m_mirroring(NameTableMirroring::Vertical),
      m_mirroringCallback(mirroring_callback)
{
}

Byte MapperGxROM::ReadPRG(Address addr)
{
    if (addr >= 0x8000)
    {
        return m_cartridge.GetROM()[(prgbank * 0x8000) + (addr & 0x7fff)];
    }
    return 0;
}

void MapperGxROM::WritePRG(Address addr, Byte value)
{
    if (addr >= 0x8000)
    {
        prgbank = ((value & 0x30) >> 4);
        chrbank = (value & 0x3);
        m_mirroring = NameTableMirroring ::Vertical;
    }
    m_mirroringCallback();
}

Byte MapperGxROM::ReadCHR(Address addr)
{
    if (addr <= 0x1FFF)
    {
        return m_cartridge.GetROM()[chrbank * 0x2000 + addr];
    }
    return 0;
}

NameTableMirroring MapperGxROM::GetNameTableMirroring()
{
    return m_mirroring;
}

void MapperGxROM::WriteCHR(Address addr, Byte value)
{
    LOG(Level::Info) << "Not expecting writes heres";
}

PNE_NAMESPACE_END

// DONE